Passed
Push — master ( efa8d0...23088e )
by Rafael S.
01:21
created

from-bytes.js ➔ readBytes   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
nc 2
nop 3
dl 0
loc 14
rs 8.8571
c 1
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A from-bytes.js ➔ ... ➔ ??? 0 1 1
1
/*
2
 * from-bytes: convert bytes to numbers and strings.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const reader = require("../src/read-bytes.js");
8
const bitDepths = require("../src/bit-depth.js");
9
const helpers = require("../src/helpers.js");
10
11
/**
12
 * Turn a byte buffer into what the bytes represent.
13
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
14
 * @param {Object} type One of the available types.
15
 * @return {!Array<number>|number|string}
16
 */
17
function fromBytes(buffer, type) {
18
    let bitDepth = type.bits;
19
    helpers.fixFloat16Endianness(buffer, type);
20
    helpers.makeBigEndian(buffer, type.be, bitDepth);
21
    bytesToInt(buffer, type.base);
22
    let values = readBytes(
23
            buffer,
24
            type,
25
            getBitReader(type)
26
        );
27
    if (type.char) {
28
        values = values.join("");
29
    }
30
    if (type.single) {
31
        values = getSingleValue(values, type);
32
    }
33
    return values;
34
}
35
36
/**
37
 * Return the first value from the result value array.
38
 * @param {!Array<number>|string} values The values.
39
 * @param {Object} type One of the available types.
40
 * @return {number|string}
41
 */
42
function getSingleValue(values, type) {
43
    if (type.char) {
44
        values = values.slice(0, type.bits / 8);
45
    } else {
46
        values = values[0];
47
    }
48
    return values;
49
}
50
51
/**
52
 * Turn a array of bytes into an array of what the bytes should represent.
53
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
54
 * @param {Object} type The type.
55
 * @param {Function} bitReader The function to read the bytes.
56
 * @return {!Array<number>|string}
57
 */
58
function readBytes(bytes, type, bitReader) {
59
    let values = [];
60
    let i = 0;
61
    let offset = type.bits < 8 ? 1 : type.bits / 8;
62
    let len = bytes.length - (offset -1);
63
    let maxBitDepthValue = bitDepths.BitDepthMaxValues[type.bits];
64
    let signFunction = type.signed && !type.float ?
65
        helpers.signed : function(x){return x;};
66
    while (i < len) {
67
        values.push(signFunction(bitReader(bytes, i, type), maxBitDepthValue));
68
        i += offset;
69
    }
70
    return values;
71
}
72
73
/**
74
 * Return a function to read binary data.
75
 * @param {Object} type One of the available types.
76
 * @return {Function}
77
 */
78
function getBitReader(type) {
79
    let bitReader;
80
    if (type.char) {
81
        bitReader = reader.readChar;
82
    } else {
83
        bitReader = reader[getReaderFunctionName(type.bits, type.float)];
84
    }
85
    return bitReader;
86
}
87
88
/**
89
 * Build a bit reading function name based on the arguments.
90
 * @param {number} bits The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64.
91
 * @param {boolean} float True if the values are IEEE floating point numbers.
92
 * @return {string}
93
 */
94
function getReaderFunctionName(bits, float) {
95
    return 'read' + (bits < 8 ? 8 : bits) +
96
        'Bit' + (float ? "Float" : "");
97
}
98
99
/**
100
 * Turn bytes to base 10.
101
 * @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings.
102
 * @param {number} base The base.
103
 */
104
function bytesToInt(bytes, base) {
105
    if (base != 10) {
106
        let i = 0;
107
        let len = bytes.length;
108
        while(i < len) {
109
            bytes[i] = parseInt(bytes[i], base);
110
            i++;
111
        }
112
    }
113
}
114
115
module.exports.fromBytes = fromBytes;
116